package com.fingard.dsp.bank.directbank.jhbank01;

import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.directbank.jhbank01.utils.PublickeyEncript;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * Created by F on 2018/7/20.
 */
public class JHBANK01Balance extends JHBANK01Base implements IBalance
{
    @Override
    public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {
        String certificatePath = getBankConfig().getValueAsString("certificatePath");
        RetAts7506BalQry retAts = new RetAts7506BalQry(p_req7506.reqHeader);
        retAts.writeStart();
        try{
            while (p_req7506.readRecord()) {
                XmlTextWriter xmlTextWriter = new XmlTextWriter();
                RetAtsBalDetail detail = new RetAtsBalDetail();
                JHBANK01Header header = new JHBANK01Header();
                header.userid=bankFront.operator;
                header.brchno = getBankConfig().getValueAsString("nodeNo");
                header.servtp ="";
                header.prcscd ="QCA01";
                writeCommenHeader(xmlTextWriter,header);
                //        tellNo	tellNo	操作员号	X	4
//        nodeNo	nodeNo	财政局标示	X	5	默认值88883
//        tranCode	tranCode	交易代码	X	7	交易码：QCA01
//        accountType	accountType	账户类型	X	1
//        accountNo	accountNo	账号	X	40
                xmlTextWriter.writeStartElement("Body");
                xmlTextWriter.writeElementString("tellNo",bankFront.operator);
                String nodeNo = getBankConfig().getValueAsString("nodeNo");
                xmlTextWriter.writeElementString("nodeNo",nodeNo,true);
                xmlTextWriter.writeElementString("tranCode","QCA01");
//                xmlTextWriter.writeElementString("accountType",convertAccountTypeToBank(p_req7506.readedDetail.savingType));
                xmlTextWriter.writeElementString("accountType","",true);
                xmlTextWriter.writeElementString("accountNo",p_req7506.readedDetail.actNum,true);
                xmlTextWriter.writeEndDocument();
                String[] bankresult = sendToBank(xmlTextWriter.toXmlString());
                if(bankresult[0].length()==0){
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(bankresult[1],tmpSbMsg);
                    if(tmpBkReader!=null){
//                        hostReturnCode	hostReturnCode	返回代码	X	4
//                        hostErrorMessage	hostErrorMessage	错误信息	X	200
//                        accountName	accountName	户名	X	60
//                        balance	balance	余额	S	16,2
                        int offset =bankresult[1].indexOf("<hostReturnCode>");
                        int endset =bankresult[1].indexOf("</hostReturnCode>")+"</hostReturnCode>".length();
                        String source  = bankresult[1].substring(offset,endset);
                         offset =bankresult[1].indexOf("<hostErrorMessage>");
                         endset =bankresult[1].indexOf("</hostErrorMessage>")+"</hostErrorMessage>".length();
                        source = source+bankresult[1].substring(offset,endset);
                        String hostReturnEncrypt = tmpBkReader.getFirstText("/root/hostReturnEncrypt");
                        boolean b= PublickeyEncript.checkSign(source, hostReturnEncrypt,certificatePath);
                        if(!b){
                            detail.respCode = "-1";
                            detail.respInfo = "验签不通过";
                        }else {
                            String hostReturnCode = tmpBkReader.getFirstText("/root/hostReturnCode");
                            if ("0000".equals(hostReturnCode)) {
                                detail.respCode = "0";
                                detail.actCur =p_req7506.readedDetail.actCur;
                                detail.actArea =p_req7506.readedDetail.actArea;
                                detail.actNum = p_req7506.readedDetail.actNum;
                                detail.currBal = tmpBkReader.getFirstText("/root/balance");
                                detail.availBal = detail.currBal;
                                detail.actCur = convertCurToATS(tmpBkReader.getFirstText("/root/currencyType"));
                            } else {
                                detail.respCode = "-1";
                                detail.respInfo = tmpBkReader.getFirstText("/root/hostErrorMessage");
                            }
                        }
                    }else {
                        detail.respCode = "-1";
                        detail.respInfo = tmpSbMsg.toString();
                    }

                }else{
                    detail.respCode ="-1";
                    detail.respInfo = bankresult[1];
                }
                retAts.writeRetDetail(detail,p_req7506.isBatch);
            }
        }catch (Exception e){
            retAts.writeRespCode("-1");
            retAts.writeRespInfo(e.getMessage());
            e.printStackTrace();
        }
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
        return null;
    }
}
